package com.xh.fma.commodity.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.xh.common.exceptiongroup.SaveGroup;
import com.xh.common.exceptiongroup.UpdateGroup;
import com.xh.common.exceptiongroup.UpdateIsshowGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.xh.fma.commodity.entity.BrandEntity;
import com.xh.fma.commodity.service.BrandService;
import com.xh.common.utils.PageUtils;
import com.xh.common.utils.R;



/**
 * 家居品牌
 *
 * @author sjf
 * @email xhyy7762@gmail.com
 * @date 2023-05-18 17:45:54
 */
@RestController
@RequestMapping("commodity/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("commodity:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("commodity:brand:info")
    public R info(@PathVariable("id") Long id){
		BrandEntity brand = brandService.getById(id);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("commodity:brand:save")
    //BindingResult result
    public R save(@Validated({SaveGroup.class}) @RequestBody BrandEntity brand){
        //if (result.hasErrors()){
        //    HashMap<String, String> map = new HashMap<>();
        //    result.getFieldErrors().forEach((item) ->{
        //        map.put(item.getField(),item.getDefaultMessage());
        //    });
        //    return R.error(400,"品牌表单数据验证有误").put("data",map);
        //}
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("commodity:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand){
		brandService.updateById(brand);
        return R.ok();
    }

    @RequestMapping("/update/isshow")
    //@RequiresPermissions("commodity:brand:update")
    public R updateIsshow(@Validated({UpdateIsshowGroup.class}) @RequestBody BrandEntity brand){
        brandService.updateById(brand);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("commodity:brand:delete")
    public R delete(@RequestBody Long[] ids){
		brandService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @GetMapping("/relation/list")
    public R listBrands(@RequestParam(value = "catId", required = true)Long catId){
        List<BrandEntity> brandEntities = brandService.listBrands(catId);
        return R.ok().put("data",brandEntities);
    }

}
